<template>
  <view class="index_container">
    <home-page ref="home" v-if="currentTab === 0" />
    <category-page v-else-if="currentTab === 1" />

    <!-- 自动化 -->
    <automation-config v-else-if="currentTab === 2" />

    <me-page v-else-if="currentTab === 3" :userInfo="userInfo" />

    <custom-tabbar />
  </view>
</template>

<script>
import customTabbar from "@/components/custom-tabbar/index.vue";
import { mapGetters } from "vuex";
import homePage from "@/pages/home/index.vue";
import categoryPage from "@/pages/category/index.vue";
import automationConfig from "@/pages/automationConfig/index";
import mePage from "@/pages/me/index.vue";
import { getUserInfo } from "../../api";

export default {
  components: {
    customTabbar,
    homePage,
    categoryPage,
    automationConfig,
    mePage,
  },
  computed: {},
  data() {
    return {
      // 页面参数
      options: {},
      // 是否已登录
      isLogin: false,
      userInfo: {},
    };
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const app = this;

    const _res = await getUserInfo()
    console.log('个人信息', _res);
    app.userInfo = _res;

    // 微信一键登陆
    // uni.login({
    //   provider: "weixin",
    //   success: (res) => {
    //     console.log(res);
    //     // 获取用户信息
    //     uni.getUserInfo({
    //       provider: "weixin",
    //       success: (res) => {
    //         console.log("getUserInfo=>", res);
    //         app.userInfo = res.userInfo;
    //       },
    //     });
    //   },
    // });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log('飞洒快乐回复', this.currentTab);
    
    if (this.currentTab === 0) {
      this.$nextTick(() => {
        this.$refs.home && this.$refs.home.onTabShow && this.$refs.home.onTabShow();
      });
    }
  },
  computed: {
    ...mapGetters(["currentTab"]),
  },
  watch: {
    currentTab(n) {
      if (n === 0) {
        this.$nextTick(() => {
          this.$refs.home && this.$refs.home.onTabShow && this.$refs.home.onTabShow();
        });
      }
    }
  },

  methods: {
    click(item) {
      console.log("item", item);
    },

    goToDeviceInfo(id) {
      uni.navigateTo({
        url: `/pages/index/deviceInfo/index?id=${id}`,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.index_container {
  height: calc(100% - 105px - 35.045rpx);
}
</style>
